package com.training.spring.controller;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/user")
public class userController {
    @RequestMapping("/login")
    public boolean login(String userName, String password, HttpServletRequest request){
        // 账号/密码不为空 & 账号/密码长度合法
        // if(userName == null || password == null || userName.length() <= 0 || password.length() <= 0) {
        //     return false;
        // }
        if(!StringUtils.hasLength(userName) || !StringUtils.hasLength(password)){
            return false;
        }
        if("admin".equals(userName) && "admin".equals(password)){
            HttpSession session = request.getSession();
            session.setAttribute("loginUser",userName);
            return true;
        }
        return false;
    }


    @RequestMapping("/getLoginUser")
    public String getLoginUser(HttpServletRequest request){
        HttpSession session = request.getSession(false);
        if(session == null) {
            return "获取登陆人失败";
        }
        return (String) session.getAttribute("loginUser");
    }
}



